package lanou.autohome.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import lanou.autohome.R;
import lanou.autohome.basetool.BaseActivity;

/**
 * Created by dllo on 16/8/29.
 * 我的 -> 登录
 */
public class LoginActivity extends BaseActivity {


    private EditText number;
    private EditText password;
    private String name;
    private String passwordEt;

    @Override
    protected int setLayout() {
        return R.layout.activity_login_me;
    }

    @Override
    protected void intiView() {
        //账号
        number = bindView(R.id.number_login_me);
        //密码
        password = bindView(R.id.password_login_me);
        //新建一个可以添加属性的文本对象
        SpannableString ss = new SpannableString("手机号/邮箱/用户名");
        SpannableString sss = new SpannableString("请输入您的账号密码");
        //新建一个属性对象 , 设置文字的大小
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);
        AbsoluteSizeSpan asss = new AbsoluteSizeSpan(12, true);
        //附加属性到文本
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sss.setSpan(asss, 0, sss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置hint , 一定要进行转换 , 否则属性会消失
        number.setHint(new SpannableString(ss));
        password.setHint(new SpannableString(sss));

        //忘记密码
        TextView wangji = bindView(R.id.wangji_login_me);
        wangji.setOnClickListener(this);

        //返回
        ImageView back = bindView(R.id.imageview_login_back);
        back.setOnClickListener(this);

        //登录
        RelativeLayout login = bindView(R.id.relativelayout_login_me);
        login.setOnClickListener(this);

        //注册
        TextView zhuce = bindView(R.id.zhuce_login_me);
        zhuce.setOnClickListener(this);
    }

    @Override
    protected void intiData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wangji_login_me://忘记密码,
                Intent intent = new Intent(getApplicationContext(), ForgetActivity.class);
                startActivity(intent);
                break;
            case R.id.imageview_login_back://返回
                Intent back= new Intent();
                setResult(1 , back);
                finish();
                break;
            case R.id.relativelayout_login_me://登录按钮
                //登录
                MyBemoUser user = new MyBemoUser();
                user.setUsername(number.getText().toString());
                user.setPassword(password.getText().toString());
                Toast.makeText(this, "正在核对信息,请稍等", Toast.LENGTH_SHORT).show();
                user.login(new SaveListener<MyBemoUser>() {
                    @Override
                    public void done(MyBemoUser myBemoUser, BmobException e) {
                        if (e == null) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                            Intent denglu = new Intent();
//                            denglu.putExtra("back" , true);
//                            setResult(1 , denglu);
//                            succeed();
                            finish();
                        } else {
                            Log.d("LoginActivity", e.toString());
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                            password.setText(null);
                            number.setText(null);
                        }
                    }
                });

                break;
            case R.id.zhuce_login_me://注册按钮
                Intent zhuce = new Intent(this, RegisterActivity.class);
                startActivityForResult(zhuce , 2);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 2) {
            if(data.getBooleanExtra("back" , false)) {
                String s = data.getStringExtra("用户名");
                String s1 = data.getStringExtra("密码");
                number.setText(s);
                password.setText(s1);
            }
        }
    }

//    public void succeed(){
//        MyBemoUser myBemoUser = BmobUser.getCurrentUser(MyBemoUser.class);
//        if(myBemoUser != null) {
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.yang51);
//            myBemoUser.setIcon(bitmap);
//            //保存
//            myBemoUser.update(new UpdateListener() {
//                @Override
//                public void done(BmobException e) {
//                    if (e == null) {
//                        Log.d("RegisterActivity", "上传头像成功");
//                    } else {
//                        Log.d("RegisterActivity", "上传头像失败" + e.toString());
//                    }
//                }
//            });
//        }
//    }
}
